using System;
using System.Linq;
using JNPF.Utils;
using System.Threading.Tasks;
using JNPF.Data;
using JNPF.Data.Util;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;
using JNPF.Basics.Models;

namespace JNPF.Basics.Logic
{
    public class CustomerinsurancecoverageService: DataFactory, CustomerinsurancecoverageIService
    {
		/// <summary>
		/// 获取信息
		/// </summary>
		/// <param name="id">主键id</param>
		/// <returns></returns>
		public async Task<CustomerinsurancecoverageEntity> GetInfo(int id)
		{
			using (var db = this.Basic)
			{
				return await db.GetEntityAsync<CustomerinsurancecoverageEntity>(m => m.Id == id);
			}
		}

		/// <summary>
		/// 列表（分页）
		/// </summary>
		/// <param name="input">请求参数</param>
		/// <returns></returns>
		public async Task<PageResult<CustomerinsurancecoverageEntity>> GetPageList(CustomerinsurancecoverageListQueryInput input)
		{
			using (var db = this.Basic)
			{
				var queryWhere = ExtLinq.True<CustomerinsurancecoverageEntity>();
				var queryParam = input.queryJson.ToObject();
                //查询类型
                if (!string.IsNullOrEmpty(input.type))
                {
                    queryWhere = queryWhere.And(m => m.Type.Contains(input.type));
                }
                //查询保险产品名称
                if (!string.IsNullOrEmpty(input.insuranceProductName))
				{
					queryWhere = queryWhere.And(m => m.InsuranceProductName.Contains(input.insuranceProductName));
				}

                if (input.customerId.HasValue)
                {
                    queryWhere = queryWhere.And(m => m.CustomerId == input.customerId.Value);
                }
                queryWhere = queryWhere.And(m => m.DeleteMark == 0);
                return await db.GetIQueryable<CustomerinsurancecoverageEntity>().Where(queryWhere).GetPageResultAsync(input);
			}
		}

		/// <summary>
		/// 创建信息
		/// </summary>
		/// <param name="entity">实体对象</param>
		/// <returns></returns>
		public async Task<int> Create(CustomerinsurancecoverageEntity entity)
		{
			using (var db = this.Basic)
			{
				return await db.InsertAsync(entity);
			}
		}

		/// <summary>
		/// 更新信息
		/// </summary>
		/// <param name="entity">实体对象</param>
		/// <returns></returns>
		public async Task<int> Update(CustomerinsurancecoverageEntity entity)
		{
			using (var db = this.Basic)
			{
				return await db.UpdateAsync(entity);
			}
		}

		/// <summary>
		/// 删除信息
		/// </summary>
		/// <param name="entity">实体对象</param>
		/// <returns></returns>
		public async Task<int> Delete(CustomerinsurancecoverageEntity entity)
		{
			using (var db = this.Basic)
			{
				return await db.DeleteAsync(entity);
			}
		}

		/// <summary>
		/// 列表
		/// </summary>
		/// <param name="input">请求参数</param>
		/// <returns></returns>
		public async Task<List<CustomerinsurancecoverageEntity>> GetList(CustomerinsurancecoverageListQueryInput input)
		{
			using (var db = this.Basic)
			{
				var queryWhere = ExtLinq.True<CustomerinsurancecoverageEntity>();
				//查询保险产品名称
				if (!string.IsNullOrEmpty(input.insuranceProductName))
				{
					queryWhere = queryWhere.And(m => m.InsuranceProductName.Contains(input.insuranceProductName));
				}
                queryWhere = queryWhere.And(m => m.DeleteMark == 0);
                return await db.GetIQueryable<CustomerinsurancecoverageEntity>().Where(queryWhere).ToListAsync();
			}
		}


    }
}